package com.example.music_mediaplayer;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.PowerManager;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Gallery;


/**
 * Created by quan on 2017/12/20.
 */

public class OnePiexlActivity extends Activity{

    private BroadcastReceiver endReceiver;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置1像素
        Window window = getWindow();
        window.setGravity(Gallery.SCROLL_INDICATOR_LEFT|Gallery.SCROLL_INDICATOR_TOP);
        WindowManager.LayoutParams params = window.getAttributes();
        params.x = 0;
        params.y = 0;
        params.height = 1;
        params.width = 1;
        window.setAttributes(params);

        //结束该页面的广播
        endReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Log.i("787878","OnePxActivity finish");
                finish();
            }
        };
        registerReceiver(endReceiver,new IntentFilter("finish activity"));
        //检查屏幕状态
        checkScreen("onCreate");
    }

    @Override
    protected void onResume() {
        super.onResume();
        checkScreen("OnResume");
    }

    /**
     * 检查屏幕状态 isScreenOn为true 屏幕“亮”结束该activity
     */
    private void checkScreen(String methodName){
        Log.i("787878","from call method"+methodName);
        PowerManager pm = (PowerManager) OnePiexlActivity.this.getSystemService(Context.POWER_SERVICE);
        boolean isScreenOn = pm.isScreenOn();
        Log.i("787878","isScreenOn"+isScreenOn);
        if (isScreenOn){
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        Log.i("787878","onDestroy");
        unregisterReceiver(endReceiver);
        super.onDestroy();
    }

}
